package com.luoxiang.socialandroid.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.luoxiang.lib_common.bean.NumberBean
import com.luoxiang.lib_common.common.Constants
import com.luoxiang.lib_common.utils.NetUtils
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.base.LazyLoadFragment
import com.luoxiang.socialandroid.bean.IllustrateBean
import com.luoxiang.socialandroid.databinding.FragmentInviteBinding
import com.luoxiang.socialandroid.dialog.ListIllustrateDialog
import com.luoxiang.socialandroid.utils.AESUtil
import com.luoxiang.socialandroid.utils.MmkvUtil
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * Author: fuxinbo

 * Date: 2023/2/9

 * Description:
 */
class InviteFragment : LazyLoadFragment() {

    private lateinit var binding: FragmentInviteBinding

    override fun setContentView(): Int = R.layout.fragment_invite

    private var mDialog: ListIllustrateDialog? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentInviteBinding.inflate(layoutInflater)
        super.onCreateView(inflater, container, savedInstanceState)
        return binding.root
    }

    override fun initView() {
        initViewPage()
        mDialog = ListIllustrateDialog(requireContext())
        binding.textIllustrate.setOnClickListener {
            mDialog?.show()
        }
    }

    override fun lazyLoad() {
        getListIllustrateData()
    }

    override fun onDestroy() {
        super.onDestroy()
        mDialog?.dismiss()
        mDialog = null
    }

    private fun initViewPage() {
        binding.viewPager.apply {
            adapter = object : FragmentStateAdapter(childFragmentManager, lifecycle) {
                override fun getItemCount(): Int = 3

                override fun createFragment(position: Int): Fragment = InviteSubFragment().apply {
                    arguments = Bundle().apply {
                        putInt(PAGE_TYPE, position)
                    }
                }
            }
        }
        ViewPager2Delegate.install(binding.viewPager, binding.tab)
    }

    private fun getListIllustrateData() {
        NetUtils.getInstance().apiService.getListIllustrateData(
            Constants.MmkvConst.EXTRA,
            MmkvUtil.decodeString(Constants.MmkvConst.TOKEN),
        ).subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<NumberBean> {
                override fun onSubscribe(d: Disposable?) {}

                override fun onError(e: Throwable?) {}

                override fun onComplete() {}

                override fun onNext(value: NumberBean?) {
                    val data = value?.data
                    var decryptStr: String? = null
                    try {
                        decryptStr = AESUtil.aesDecryptString(data, Constants.MmkvConst.KEY)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                    LogUtils.i("decryptStr10", decryptStr)
                    val illustrate = Gson().fromJson<IllustrateBean>(
                        decryptStr,
                        object : TypeToken<IllustrateBean>() {}.type
                    )
                    mDialog?.setList(illustrate.data)
                }
            })
    }
}